package com.jaay.common;

import lombok.Data;

@Data
public class CommonResult<T> {
    protected String msg;
    protected int code;
    protected T data;


    public CommonResult() {


    }

    public CommonResult(String msg, int code, T data) {

        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    public CommonResult(Builder<T> builder) {

        this.msg = builder.msg;
        this.code = builder.code;
        this.data = builder.data;


    }


    public CommonResult<T> success(T data) {

        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(Result.SUCCESS.getCode());
        commonResult.setMsg(Result.SUCCESS.getMsg());
        commonResult.setData(data);

        return commonResult;

    }

    public CommonResult<T> success() {

        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(Result.SUCCESS.getCode());
        commonResult.setMsg(Result.SUCCESS.getMsg());

        return commonResult;

    }

    public CommonResult<T> fail() {

        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(Result.FAIL.getCode());
        commonResult.setMsg(Result.FAIL.getMsg());

        return commonResult;

    }

    public CommonResult<T> fail(T obj) {

        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(Result.FAIL.getCode());
        commonResult.setMsg(Result.FAIL.getMsg());
        commonResult.setData(obj);

        return commonResult;

    }


    public static class Builder<T> {

        private String msg;
        private int code;
        private T data;

        public Builder<T> msg(String msg) {

            this.msg = msg;
            return this;
        }

        public Builder<T> code(int code) {

            this.code = code;
            return this;

        }

        public Builder<T> data(T t) {
            this.data = t;

            return this;
        }


        public CommonResult<T> build() {

            return new CommonResult<>();
        }

    }

}
